package com.nwpusp.display.views.components;

import java.util.Timer;
import java.util.TimerTask;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;

import com.example.petssports.R;
import com.nwpusp.display.GlobalVar;
import com.nwpusp.display.MainActivity;

public class GameMillisecondClock extends View{
	private Bitmap bgBitmap;
	private Bitmap retBitmap;
	private Paint fontPaint;
	public float x;
	public float y;
	public float scale;
	public float width;
	public float height;
	private Timer timer;
	private TimerTask task;
	private int minutes = 0;
	private int second = 0;
	private int millisecond = 0;
	private long temp = 0;
	private int secondtemp = 0;
	private String timerStr = "00"+" : "+"00"+" : "+ "00";
	private String minutesTempStr = "";
	private String secondTempStr = "";
	private String milliTempStr = "";
	private float strWidth;
	private float strHeight;
	private long StartTime = 0;
	private long SysTime = 0;
	private long timeDiff = 0;
	
	public GameMillisecondClock(MainActivity mainActivity){
		super(mainActivity);
		getBitmapSource();
		fontPaint = new Paint();
		fontPaint.setTextSize(GlobalVar.screenHeight/16);
		fontPaint.setColor(0xFFE7019C);
		fontPaint.setAntiAlias(true);
		strWidth =  fontPaint.measureText(timerStr);
		strHeight =  (-fontPaint.getFontMetrics().descent - fontPaint.getFontMetrics().ascent);
		width = strWidth*((float)1.5);
		height = strHeight*((float)2);
		bgBitmap = Scale(bgBitmap,(int)width,(int)height);
		retBitmap = bgBitmap;
	}

	
	public void startTimer(){
		timer = new Timer();
		task = getNewTimerTask();
		timer.schedule(task,0, 10);
		StartTime = System.nanoTime();
	}
	
	public void stopTimer(){
		timer.cancel();
	}
	
	public int getMillisecond () {
		return millisecond;
	}
	
	public int getSecond() {
		return second;
	}
	
	public int getMinutes() {
		return minutes;
	}
	
	 public TimerTask getNewTimerTask(){
		TimerTask task = new TimerTask(){ 
			public void run() { 
				SysTime = System.nanoTime();
				timeDiff = SysTime - StartTime;
				temp = timeDiff / 10000000;
				millisecond = (int)temp % 100;
				secondtemp = (int)temp / 100;
				second = secondtemp % 60;
				minutes = secondtemp / 60;
				if(minutes<10){
					minutesTempStr = "0"+ String.valueOf(minutes);
				}
				else{
					minutesTempStr = String.valueOf(minutes);
				}
				if(second<10){
					secondTempStr = "0"+ String.valueOf(second);
				}
				else{
					secondTempStr = String.valueOf(second);
				}
				if(millisecond<10){
					milliTempStr = "0"+ String.valueOf(millisecond);
				}
				else{
					milliTempStr = String.valueOf(millisecond);
				}
				timerStr = minutesTempStr+" : "+secondTempStr+" : "+ milliTempStr;
				retBitmap = DrawClock(bgBitmap, timerStr);
			}
		}; 
		return task;
	}
	
	public void setLocation(float x ,float y){
		this.x = x;
		this.y = y;
	}
	
	private void getBitmapSource(){
		bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gameviewnamebar);
	}
	
	private Bitmap DrawClock( Bitmap SourceBitmap, String nameStr) {
		Bitmap NewBitmap = Bitmap.createBitmap((int)width,(int)height, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas();
		canvas.setBitmap(NewBitmap);
		canvas.drawBitmap(SourceBitmap, 0, 0, null);
		canvas.drawText(nameStr, (width - strWidth) / 2, (height + strHeight) / 2, fontPaint);
		return NewBitmap;
	}
	
	private Bitmap Scale(Bitmap bitmap,int newWidth,int newHeight){
		Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap,newWidth,newHeight,true); 
	    return resizedBitmap;
	}
		
	public Bitmap draw(){
		return retBitmap;
	}
}